<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\30 0030
 * Time: 10:07
 */

namespace app\Lib\Model\Tag;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\Img\Img;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\StyleTag;
use app\Lib\Model\User\User;

abstract class Tag
{
    public $cacheName;
    /** @var StyleTag $table */
    public $table;
    protected $tagId;
    protected $cache;

    /**
     * 获取tagId
     * @return int
     */
    public function getTagId()
    {
       return $this->tagId;
    }

    /**
     * 获取标签名称
     * @return string
     */
    public function getTagName()
    {
       return $this->getFiledValue($this->table->_tag_name);
    }

    /**
     * 获取标签名称
     * @return string
     */
    public function getTips()
    {
        return $this->getFiledValue($this->table->_tips);
    }

    /**
     * 获取创建人类
     * @return UserHandle
     */
    public function getCreateUidClass()
    {
        $uid = $this->getFiledValue($this->table->_create_id);
        return new User($uid);
    }

    /**
     * 获取创建时间
     * @return string
     */
    public function getCreateTime()
    {
        return $this->getFiledValue($this->table->_create_time);
    }

    /**
     * 修改人类
     * @return UserHandle
     */
    public function getUpdateUidClass()
    {
        $uid = $this->getFiledValue($this->table->_update_id);
        return new User($uid);
    }

    /**
     * 获取修改时间
     * @return string
     */
    public function getUpdateTime()
    {
        return $this->getFiledValue($this->table->_update_time);
    }

    /**
     * 获取img图片类
     * @return Img
     */
    public function getImgClass()
    {
        $img = $this->getFiledValue($this->table->_img_id);
        if($img && $img!='无'){
            return new \app\Lib\Model\Img\Img($img);
        }
        return '无';
    }

    /**
     * 获取中文状态
     * @return string
     */
    public function getTagStatusStr()
    {
        $dataString = [
            '0'=>'禁用',
            '1'=>'正常',
        ];
        return $dataString[$this->getTagStatus()];
    }

    private function getTagStatus(){
        return $this->getFiledValue($this->table->_tag_status);
    }

    /**
     * 获取指定字段数据
     * @param $filed
     * @return string
     */
    protected function getFiledValue($filed){
        $result = false;
        /*是否开启缓存*/
        if($this->cache){
            /*获取缓存值*/
            $result = $this->getCacheValue($filed);
        }
        /*没有开启缓存*/
        if($result === false){
            /*获取数据库值*/
            $result = $this->getValue($filed);
        }
        return $result;
    }

    /**
     * 获取缓存
     * @param $filed
     * @return string
     */
    private function getCacheValue($filed){
        /*获取统一的缓存名称*/
        $cacheName = $this->getCacheName($filed);
        $result = cache($cacheName);
        /*判断缓存是否存在*/
        if(empty($result)){
            /*不存在生成缓存*/
            $result = $this->getValue($filed);
            $result = empty($result)?'无':$result;
            cache($cacheName,$result,1800);
        }
        return $result;
    }

    /**
     * 获取数据值
     * @param $filed
     * @return string
     */
    private function getValue($filed){
        $model = $this->table;
        $value = $model->where($model->_id,$this->tagId)->value($filed);
        return $value;
    }

    /**
     * 获取缓存名称
     * @param $filed
     * @return string
     */
    protected function getCacheName($filed){
        return $this->cacheName.'_'.$filed;
    }

    /**
     * 删除单个缓存
     * @param $filed
     * @throws BlException
     */
    public function delSingleCache($filed){
        if(empty($filed)){
            throw new BlException('请提供要清除缓存的字段');
        }
        cache($this->getCacheName($filed),null);
    }

    /**
     * 删除全部缓存
     * @return bool
     * @throws BlException
     */
    public function delAllCache(){
        $result =  $this->table->getTableFields();
        foreach ($result as $val){
            $this->delSingleCache($val);
        }
        return true;
    }

}